package com.hnu.highlynewapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hnu.highlynewapp.dao.IndexDao;
import com.hnu.highlynewapp.dao.PolicyDao;
import com.hnu.highlynewapp.entity.IndexEntity;
import com.hnu.highlynewapp.entity.PolicyEntity;
import com.hnu.highlynewapp.enums.IndexEnum;
import com.hnu.highlynewapp.service.IndexService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kLjSumi
 * @Date 2021/9/19 2:02
 */
@Service("indexService")
public class IndexServiceImpl extends ServiceImpl<IndexDao, IndexEntity> implements IndexService {
    @Override
    public List<Map<String, Object>> getIndexList(String indexName) {
        List<IndexEntity> indexEntities = this.list();
        Long indexId=-1L;
        for (IndexEntity indexEntity : indexEntities) {
            if (indexEntity.getName().equals(indexName)) {
                indexId = indexEntity.getId();
                break;
            }
        }
        Long finalIndexId = indexId;
        List<Map<String, Object>> result = indexEntities.stream().filter(o->o.getParentId() == finalIndexId)
                .map(o->{
                    Map<String, Object> map = new HashMap<>();
                    map.put("keys", indexEntities.stream().filter(p -> p.getParentId() == o.getId())
                            .map(IndexEntity::getName).collect(Collectors.toList()));
                    map.put("listName", o.getName());
                    return map;
                }).collect(Collectors.toList());
        return result;
    }

    public List<String> getKeysByListName(String listName) {
        List<IndexEntity> indexEntities = list();
        Long listId=-1L;
        for (IndexEntity indexEntity : indexEntities) {
            if (indexEntity.getName().equals(listName)) {
                listId = indexEntity.getId();
                break;
            }
        }
        Long finalListId = listId;
        List<String> result = indexEntities.stream().filter(o -> o.getParentId() == finalListId)
                .map(IndexEntity::getName).collect(Collectors.toList());
        return result;
    }
}
